<template>
  <span v-if="!isNetClientType">
    <a-badge :color="isRuning ? 'green' : 'red'" :text="isRuning ? '运行中' : '停止'" style="margin-left: 10px"/>
    <a-popconfirm
      title="确认启动网络服务？"
      @confirm="runNetwork('start')"
      v-if="!isRuning">
      <a style="font-size: 12px;margin-left: 10px;">启动网络服务</a>
    </a-popconfirm>
    <a-popconfirm
      title="确认停止网络服务？"
      @confirm="runNetwork('stop')"
      v-if="isRuning">
      <a style="font-size: 12px;margin-left: 10px;">停止网络服务</a>
    </a-popconfirm>
  </span>
</template>

<script>
import { runNetwork } from '@/views/iot/product/api.js'
export default {
  name: 'NetworkRun',
  props: {
    productId: {
      type: String,
      default: null
    },
    network: {
      type: Object,
      default: () => {}
    }
  },
  components: {
  },
  data () {
    return {
    }
  },
  created () {
  },
  computed: {
    isRuning () {
      return this.network.state === 'runing'
    },
    isNetClientType () {
      return this.network.type === 'TCP_CLIENT' || this.network.type === 'MQTT_CLIENT'
    }
  },
  methods: {
    runNetwork (state) {
      return runNetwork(this.productId, state).then((resp) => {
        if (resp.success) {
          this.$message.success('操作成功')
          this.$emit('success')
        }
        return resp
      })
    }
  }
}
</script>

<style lang="less" scoped>
</style>
